package com.android.ailaoshan.index.config;

import android.content.Context;
import android.location.LocationManager;
import android.support.design.widget.FloatingActionButton;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.android.ailaoshan.R;
import com.android.ailaoshan.index.MainActivity;
import com.android.ailaoshan.index.listener.ButtonClickListener;
import com.android.ailaoshan.index.listener.GPSStatusListener;
import com.android.ailaoshan.index.listener.MapScaleListener;
import com.esri.arcgisruntime.layers.ArcGISMapImageLayer;
import com.esri.arcgisruntime.loadable.LoadStatus;
import com.esri.arcgisruntime.mapping.ArcGISMap;
import com.esri.arcgisruntime.mapping.Basemap;
import com.esri.arcgisruntime.mapping.view.MapView;
import com.qmuiteam.qmui.widget.QMUILoadingView;

/**
 * 配置地图显示
 * 显示地图控件
 */
public class MapConfigure {

    private Context mContext;
    private MainActivity mainActivity;

    private QMUILoading qmuiLoading;

    private MapView mMapView;
    private ArcGISMap arcGISMap;

    private TextView gpsStatus;
    private LocationManager mLocationManager;
    private GPSStatusListener gpsStatusListener;

    private ButtonClickListener buttonClickListener;

    private FloatingActionButton locationBtn;
    private ImageButton zoomIn;
    private ImageButton zoomOut;
    private FloatingActionButton myJobBtn;
    private FloatingActionButton camera;
    private FloatingActionButton microphone;

    public MapConfigure(Context mContext,MainActivity mainActivity) {
        this.mContext = mContext;
        this.mainActivity = mainActivity;
    }

    public void initMap(){
        if(this.mainActivity != null){

            // 显示正在加载
            QMUILoadingView loadingView = mainActivity.findViewById(R.id.qmui_loading);
            qmuiLoading = new QMUILoading(loadingView,mContext);
            qmuiLoading.startLoading();

            this.mMapView = this.mainActivity.findViewById(R.id.MapView_center_container);

            // 加载底图
            arcGISMap = new ArcGISMap(Basemap.Type.IMAGERY,
                            25.063463,102.758171,16);
            final String layerURL = "http://202.203.134.147:6080/arcgis/rest/services/AilaoFeature/AilaoFeatureService/MapServer";
            final ArcGISMapImageLayer mapImageLayer = new ArcGISMapImageLayer(layerURL);
            arcGISMap.getOperationalLayers().add(mapImageLayer);

            // 显示地图
            if(mMapView != null){
                mMapView.setMap(arcGISMap);
            }
        }
    }

    /**
     * 地图加载监听
     */
    public void mapListener(){

        if(arcGISMap != null){
            arcGISMap.addDoneLoadingListener(
                ()-> {
                    if(arcGISMap.getLoadStatus().equals(LoadStatus.LOADED)){

                        // 停止显示正在加载
                        qmuiLoading.stopLoading();

                        addViewListener();

                    }
                }
            );
        }
    }

    /**
     * 添加控件监听
     */
    private void addViewListener(){
        // GPS状态监听
        gpsStatus = mainActivity.findViewById(R.id.gps_status_txt);
        gpsStatus.setVisibility(View.VISIBLE);
        mLocationManager = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);
        if(mLocationManager != null){
            if(mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
                gpsStatusListener = new GPSStatusListener(gpsStatus,mLocationManager);
                mLocationManager.addGpsStatusListener(gpsStatusListener);
            }else{
                Toast.makeText(mContext,"GPS不可用，请打开GPS开关",Toast.LENGTH_LONG).show();
            }
        }

        // 按钮监听
        // 定位按钮
        buttonClickListener = new ButtonClickListener(mMapView,mContext,mainActivity);
        locationBtn = mainActivity.findViewById(R.id.floatingActionButton);
        locationBtn.show();
        locationBtn.setOnClickListener(buttonClickListener);
        // 任务按钮
        myJobBtn = mainActivity.findViewById(R.id.my_job);
        myJobBtn.show();
        myJobBtn.setOnClickListener(buttonClickListener);
        // 地图放大按钮
        zoomIn = mainActivity.findViewById(R.id.mapview_zoom_in);
        zoomIn.setVisibility(View.VISIBLE);
        zoomIn.setOnClickListener(buttonClickListener);
        // 地图缩小按钮
        zoomOut = mainActivity.findViewById(R.id.mapview_zoom_out);
        zoomOut.setVisibility(View.VISIBLE);
        zoomOut.setOnClickListener(buttonClickListener);
        // 拍照按钮
        camera = mainActivity.findViewById(R.id.FloatingToTakePhoto);
        camera.show();
        camera.setOnClickListener(buttonClickListener);
        // 录音按钮
        microphone = mainActivity.findViewById(R.id.takeVoice);
        microphone.show();
        microphone.setOnClickListener(buttonClickListener);

        // 地图比例监听
        mMapView.addMapScaleChangedListener(new MapScaleListener(zoomIn,zoomOut,mMapView));
    }


    /**
     * 资源释放
     */
    public void releaseSource(){
        if(gpsStatusListener != null){
            mLocationManager.removeGpsStatusListener(gpsStatusListener);
        }
        gpsStatusListener = null;
        mLocationManager = null;
        gpsStatus = null;

        locationBtn = null;
        myJobBtn = null;
        zoomIn = null;
        zoomOut = null;
        camera = null;
        microphone = null;

        buttonClickListener = null;

        arcGISMap = null;
        mMapView = null;
        qmuiLoading = null;
        mainActivity = null;
        mContext = null;
    }

    public void onPause(){
        if(mMapView != null){
            mMapView.pause();
        }
    }

    public void onDestroy(){
        if(mMapView != null){
            mMapView.dispose();
        }
    }
}
